#!/bin/bash

# 确保脚本以root或具有sudo权限的用户运行
if [[ $EUID -ne 0 ]]; then
   echo "此脚本必须以root用户或使用sudo运行" 
   exit 1
fi

echo "开始部署PbootCMS应用..."

# 检查Docker是否已安装
if ! command -v docker &> /dev/null; then
    echo "Docker未安装，正在安装..."
    yum install -y yum-utils
    yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    yum install -y docker-ce docker-ce-cli containerd.io
    systemctl start docker
    systemctl enable docker
    echo "Docker安装完成"
else
    echo "Docker已安装"
fi

# 检查Docker Compose是否已安装
if ! command -v docker-compose &> /dev/null; then
    echo "Docker Compose未安装，正在安装..."
    curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    chmod +x /usr/local/bin/docker-compose
    ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
    echo "Docker Compose安装完成"
else
    echo "Docker Compose已安装"
fi

# 确保data目录有足够的权限
echo "设置文件权限..."
chmod -R 755 ./
chmod -R 777 ./data
chmod -R 777 ./runtime
chmod -R 777 ./config

# 启动服务
echo "启动Docker容器..."
docker-compose down
docker-compose up -d --build

echo "PbootCMS应用部署完成！"
echo "可以通过 http://服务器IP:808 访问网站"
echo "MySQL数据库可以通过端口13306访问，用户名：pbootcms，密码：pbootcms123" 